package com.plsConsole.mainConsole;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import com.pls.service.PeopleLocatorSystem;
import com.plsContracts.dataContracts.DeleteEmployeeSeatRequest;
import com.plsContracts.dataContracts.DeleteEmployeeSeatResponse;
import com.plsContracts.dataContracts.GetProjectsRequest;
import com.plsContracts.dataContracts.GetProjectsResponse;
import com.plsContracts.dataContracts.GetQuadrantRequest;
import com.plsContracts.dataContracts.GetQuadrantResponse;
import com.plsContracts.dataContracts.SearchEmployeeRequest;
import com.plsContracts.dataContracts.SearchEmployeeResponse;
import com.plsContracts.dataContracts.UpdateEmployeeSeatRequest;
import com.plsContracts.dataContracts.UpdateEmployeeSeatResponse;
import com.plsDTO.dataObjects.Employee;
import com.plsDTO.dataObjects.EmployeeWorkstation;
import com.plsDTO.dataObjects.Project;
import com.plsDTO.dataObjects.Seat;
import com.plsDTO.validation.ValidationError;
import com.plsData.data.EmployeeData;

public class PeopleLocatorSystemConsole {
	public static void main(String[] args) {

		PeopleLocatorSystem pls = new PeopleLocatorSystem();

		printWelcomeMessage();
		String userInput = "";
		int choice = 0;
		do {

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					System.in));
			try {
				writeMenu();
				userInput = reader.readLine();
				choice = Integer.parseInt(userInput);
				// search employees
				if (choice == 1) {
					System.out
							.println("Please enter Employee's Id to search seat assignment.");
					String employeeId = reader.readLine();
					int ID = Integer.parseInt(employeeId);
					SearchEmployeeRequest searchRequest = new SearchEmployeeRequest();
					searchRequest.setEmployeeId(ID);

					SearchEmployeeResponse searchResponse = pls
							.searchEmployee(searchRequest);
					// returns employee details and seat map
					printWorkstationMap(
							searchResponse.getEmployeeWorkstation(),
							searchResponse.getSeatMap());

				}
				//updates seat
				if (choice == 2) {
					System.out
							.println("Please enter Employee's Id to update seat.");
					String employeeId = reader.readLine();
					int ID = Integer.parseInt(employeeId);
					System.out
							.println("Please enter seat code for the seat assignment.");
					String seatCode = reader.readLine();

					UpdateEmployeeSeatRequest updateRequest = new UpdateEmployeeSeatRequest();
					updateRequest.setEmployeeId(ID);
					updateRequest.setSeatCode(seatCode);
					UpdateEmployeeSeatResponse updateResponse = pls
							.updateEmployeeSeat(updateRequest);
					if (updateResponse.isSuccesful() == false) {
						System.out.println("Seat updated successfully.");

					} else {
						System.out.println("Seat update failed.");
					}

				}
				//deletes seat
				if (choice == 3) {
					System.out
							.println("Please enter Employee's Id to delete seat.");
					String employeeId = reader.readLine();
					int ID = Integer.parseInt(employeeId);
					System.out
							.println("Please enter seat code for the seat being deleted.");
					String seatCode = reader.readLine();
					DeleteEmployeeSeatRequest deleteRequest = new DeleteEmployeeSeatRequest();
					deleteRequest.setEmployeeId(ID);
					deleteRequest.setSeatCode(seatCode);
					DeleteEmployeeSeatResponse deleteResponse = pls
							.deleteEmployeeSeat(deleteRequest);
					if (deleteResponse.isSuccessful() == true) {
						System.out.println("Seat deleted successfully.");

					} else {
						System.out
								.println("Cannot delete seat. Employee has no assigned seat yet.");
					}
				}
			} catch (NullPointerException e) {
				System.out.println("Invalid selection.");
			} catch (NumberFormatException e) {
				System.out.println("Invalid input. Try again.");
			}

			catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {

			}
		} while (choice != 4 && choice < 4 && choice != 0);
		{
			System.out.println("Thank you for using PLS. Program terminated.");
		}

	}
//prints welcome message
	static void printWelcomeMessage() {
		System.out.println("*********   " + "  **       " + "   ******* ");
		System.out.println("**       ** " + "  **       " + "  ***     **");
		System.out.println("**       ** " + "  **       " + "   ****      ");
		System.out.println("**       ** " + "  **       " + "     ****    ");
		System.out.println("**********  " + "  **       " + "       ****  ");
		System.out.println("**          " + "  **       " + "        **** ");
		System.out.println("**          " + "  ******** " + "  **   *****  ");
		System.out.println("**          " + "  ******** " + "   *******   ");
		System.out.println("       PEOPLE LOCATOR SYSTEM          ");

	}
//prints menu
	static void writeMenu() {
		System.out.println("\nMENU");
		System.out.println("[1] Search Employee by Id");
		System.out.println("[2] Update Employee Seat");
		System.out.println("[3] Delete Employee Seat");
		System.out.println("[4] Exit");

	}
//prints seat map
	private static boolean printWorkstationMap(EmployeeWorkstation empWork,
			ArrayList<EmployeeWorkstation> workStationMap) {

		ArrayList<Project> projects = empWork.getProjectList();
		Employee employee = empWork.getEmployee();
		Seat seat = empWork.getSeatDetails();

		System.out.println("Seat is occupied by:");

		// creates the file
		try {

			// Write the project
			System.out.println("Name \t\t: " + employee.getFirstName() + " "
					+ employee.getLastName());
			System.out.print("Project \t: ");
			for (int i = 0; i < projects.size(); i++) {

				System.out.print(projects.get(i).getProjectName());
				if (i != projects.size() - 1) {
					System.out.print(",");
				}
			}
			System.out.println();
			System.out.print("Seat Code \t: " + seat.getSeatCode() + "\n");
			System.out.print("Seat Type \t: " + seat.getSeatType() + "\n");
			System.out.print("Quadrant \t: " + seat.getQuadrant() + "\n");
			System.out.print("Local No. \t: " + seat.getLocalNumber() + "\n");
			System.out.print("\n\n");

			// write the seat map

			System.out.println("Workstation Map\n");

			for (int x = 0; x < 25; x = x + 5) {
				for (int i = x; i < x + 5; i++) {
					System.out.print(workStationMap.get(i).getSeatDetails()
							.getSeatCode());
					System.out.print("     ");
				}
				System.out.print("\n");
				for (int i = x; i < x + 5; i++) {
					if (workStationMap.get(i).getEmployee().getEmployeeId() == empWork
							.getEmployee().getEmployeeId()) {
						System.out.print("*"
								+ workStationMap.get(i).getEmployee()
										.getEmployeeId() + "*");
						System.out.print("       ");
					} else if (workStationMap.get(i).getEmployee()
							.getEmployeeId() != 0) {
						System.out.print(workStationMap.get(i).getEmployee()
								.getEmployeeId()
								+ "");
						System.out.print("         ");
					} else {
						System.out.print("NA    ");
						System.out.print("         ");
					}

				}

				System.out.print("\n");
				for (int i = x; i < x + 5; i++) {

					if (workStationMap.get(i).getSeatDetails().getLocalNumber() != 0) {
						System.out.print(workStationMap.get(i).getSeatDetails()
								.getLocalNumber()
								+ "");
						System.out.print("          ");
					} else {
						System.out.print("--   ");
						System.out.print("          ");
					}
				}
				System.out.print("\n\n");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			return false;
		}
		return true;

	}
}
